// pages/home/marketing/marketing.js
const API = require('../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tagList:[ // '海报', '资讯', '朋友圈', '园区活动'
          {
            name: '海报',
            content: '多种营销类海报定制使用。'
          },
          {
            name: '资讯',
            content: '热点资讯，福利政策，一键分享。'
          },
          {
            name: '朋友圈',
            content: '转发朋友圈，精准触达。'
          },
          {
            name: '园区活动',
            content: '园区活动，在线参与。'
          }
        ],
        tagIndex: 0,
        tagChildrenList:['全部'],
        wechattagChildrenList:['全部', '获客赚佣', '推荐注册'],
        tagChildrenIndex: 0,
        wechatZoneExplainShow: true,
        pageSize: 1,
        readchBottomShow: false,
        modeIndex: '1'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            author_img: app.globalData.userInfo.headimgUrl
        })
        this.get_poster_classifyList()
        this.setData({
          modeIndex: wx.getStorageSync('marketing-guidance') ? null : '1'
        })
    },

    modeEvent () {
      let modeIndex = this.data.modeIndex;
      modeIndex = Number.isInteger(parseInt(modeIndex)) ? parseInt(modeIndex) + 1 : '0'
      if (modeIndex > 5) {
        modeIndex = null;
        wx.setStorageSync('marketing-guidance', true)
      }
      this.setData({ modeIndex })
    },
    /**
     * reachbottom
     */
    onReachBottom:function(){
        if( this.data.readchBottomShow ){
            return
        }
        if( this.data.tagIndex == 0 ){ //banner
            let pageSize1 = this.data.pageSize1
            pageSize1++
            this.setData({
                pageSize1
            })
            this.get_material_list( this.data.material_list[this.data.tagChildrenIndex] ) 
        }else if( this.data.tagIndex == 1 ){ //news

        }else if( this.data.tagIndex == 2 ){ //wechat

        }else if( this.data.tagIndex == 3 ){ //active

        }
    },

    /**
     * events - tag change
     */
    tagChange:function(e){
        this.setData({
            tagIndex: e.currentTarget.id,
            tagChildrenIndex: 0,
            pageSize: 1,
        })
        if( e.currentTarget.id == 0 ){
          console.log("this.data.material_list----", this.data.material_list)
            this.get_material_list( this.data.material_list[0] ) 
        }else if( e.currentTarget.id == 1 ){
            this.get_artcle_list()
        }else if( e.currentTarget.id == 2 ){
            this.get_wechat_zone_text_list()
            this.get_wechat_zone_text_history()
        }
    },

    /**
     * events - tagChildren Change
     */
    tagChildrenChange:function(e){
        this.setData({
            tagChildrenIndex: e.currentTarget.id
        })
    },

    /**
     * events - tagChildren Change for banner
     */
    bannerTagChildrenChange:function(e){
        this.setData({
            tagChildrenIndex: e.currentTarget.id
        })
        
        this.get_material_list( this.data.material_list[e.currentTarget.id] ) 
    },

    /**
     * events -  tagChildren Change for news
     */
    newsTagChildrenChange:function(e){
        this.setData({
            tagChildrenIndex: e.currentTarget.id
        })
        
    },

    /**
     * events - tagChildren Change for wechat
     */
    wechatTagChildrenChange:function(e){
        this.setData({
            tagChildrenIndex: e.currentTarget.id
        })
        this.get_wechat_zone_text_list(e.currentTarget.id)
    },

    /**
     * route - News Detail
     */
    routeNewsDetail:function(e){
        wx.navigateTo({
          url: '/pages/home/marketing/news/detail?id=' + e.currentTarget.dataset.item.id,
        })
    },

    /**
     * route - banner canvas detail
     */
    routeBannerCanvas:function(e){
        let item = e.currentTarget.dataset.item
        // console.log(item)
        wx.navigateTo({
        url: '/pages/share/canvas/canvas?id=' + item.categoryId
        })
    },

    /**
     * route - intro img
     */
    routeIntro:function(){
        wx.navigateTo({
          url: './intro/intro',
        })
    },

    /**
     * route - active detail
     */
    routeActiveDetail:function(e){
        wx.navigateTo({
          url: '/pages/home/marketing/active1/marketing',
        })
    },

    /**
     * events - explain show hidden
     */
    wechatZoneExplainShow:function(){
        if( this.data.wechatZoneExplainShow ){
            this.setData({
                wechatZoneExplainShow: false
            })
        }else{
            this.setData({
                wechatZoneExplainShow: true
            })
        }
    },

    /**
     * request - banner category list
     */
    get_poster_classifyList:function(){
        let that = this
        API.get_poster_classifyList({
            mediaType:5,
            type: 1
        }).then( res => {
            if( res.code == 200 ){
                that.setData({
                    material_list: res.rows
                })
                that.get_material_list( res.rows[0] ) 
            }
        })
    },

    /**
     * request - banner list
     */
    get_material_list:function(item){
        let that = this
        wx.showLoading({
            title: '加载中',
        })
        API.get_material_list({
            categoryId: item.id,
            mediaType: item.mediaType,
            pageNum: that.data.pageSize,
            pageSize: 30
        }).then( res => {
            if( res.code == 200 ){
                that.setData({
                    bannerList: res.rows
                })
            }
            setTimeout( () => {
                wx.hideLoading()
            }, 1000)
        })
    },

    /**
     * request - article list
     */
    get_artcle_list:function(){
      let that = this
      wx.showLoading({
        title: '加载中',
      })
      API.get_artcle_list({
        sign:0,//0:小程序文章 1：园区文章
        pageNum: that.data.pageSize,
        pageSize: 10
      }).then( res => {
        if( res.code == 200 ){
          that.setData({
            article_list:res.rows
          })
          if( res.rows.length < 10){
            that.setData({
                readchBottomShow: true
            })
          }
        }
        setTimeout( () => {
          wx.hideLoading()
        }, 1000)
      })
    },

    /**
     * request - wechat zone list
     */
    get_wechat_zone_text_list:function(type=''){
      let that = this
      wx.showLoading({
        title: '加载中',
      })
      API.get_wechat_zone_text_list({
        type: type == 0 ? '' : type,
        pageNum: that.data.pageSize,
        pageSize: 10
      }).then( res => {
        if( res.code == 200 ){
          that.setData({
            wechat_zone_text_list:res.rows
          })
          if( res.rows.length < 10){
            that.setData({
                readchBottomShow: true
            })
          }
        }
        setTimeout( () => {
          wx.hideLoading()
        }, 1000)
      })
    },

    /**
     * request - wechat zone share records
     */
    get_wechat_zone_text_history:function(){
      let that = this
      API.get_wechat_zone_text_history({
        pageNum: 1,
        pageSize: 10
      }).then( res => {
        if( res.code == 200 ){
            let rows = res.rows.map( ele => {
                ele.userName = ele.userName.substr(0,1)
                return ele
            })
            that.setData({
                wechat_zone_text_records_list: rows
            })
        }
        setTimeout( () => {
          wx.hideLoading()
        }, 1000)
      })
    },

    /**
     * events - wechat zone copy text
     */
    copyWechatZone:function(e){
      let that = this
      wx.setClipboardData({
        data: e.currentTarget.dataset.item.momentContent,
        success (res) {
          
        }
      })

      API.creat_wechat_zone_text_history({
        id: e.currentTarget.dataset.item.id,
        userId: app.globalData.userInfo.id
      })
    },

    /**
     * events save image
     */
    saveImg:function(e){
        wx.previewImage({
            urls: [e.currentTarget.dataset.url] 
        })
    },

})